package main

import (
	"fmt"

	"encoding/hex"

	MQTT "github.com/eclipse/paho.mqtt.golang"
)

const TOPIC = "/fw/alphair2/+/up"

var f MQTT.MessageHandler = func(client MQTT.Client, msg MQTT.Message) {

	fmt.Printf("Recived TOPIC: %s\n", msg.Topic())
	fmt.Printf("Received MSG: %s\n", hex.EncodeToString(msg.Payload()))
}

func main() {

	opts := MQTT.NewClientOptions().AddBroker("tcp://120.27.99.104:1883")

	opts.SetClientID("mqtt-iotplc")

	opts.SetDefaultPublishHandler(f)

	c := MQTT.NewClient(opts)

	if token := c.Connect(); token.Wait() && token.Error() != nil {
		panic(token.Error())
	}

	if token := c.Subscribe(TOPIC, 0, nil); token.Wait() && token.Error() != nil {
		fmt.Println(token.Error())
	}

	iot_db()

	rest_api()
}
